<template>
  <div class="template_detail">
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3" style="line-height: 25px;">模板标题：</el-col>
      <el-col :span="6">
        <el-input
          type="input"
          placeholder="请输入内容"
          v-model="currEditInfo.title"
        />
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">模板图片：</el-col>
      <el-col :span="20">
        <el-upload
          action="http://47.103.2.155:8801/api/File/upload"
          list-type="picture-card"
          name="file"
          :on-success="(obj,file,list) => onUpload(obj,file,list,idx)"
          :limit="6"
          :file-list="imageList"
          :class="{ hide: hideUploadBtn }"
          :on-remove="onRemoveImageList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">模板说明：</el-col>
      <el-col :span="20">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="currEditInfo.content"
        />
      </el-col>
    </el-row>
    <el-row :span="24" style="display: flex;justify-content: center;">
      <el-col :span="6" style="text-align: center;">
        <el-button type="primary" @click="onSave">保存</el-button>
        <el-button type="primary" plain @click="onClose">关闭</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { saveTemplateDataList } from '@/api/yzNote';


  export default {
    name: 'templateDetail',
    props: {
      editType: '',
      editInfo: {},
    },
    data() {
      return {
        imageList: [],
        currEditInfo: {},
        hideUploadBtn: false
      };
    },
    beforeDestroy() {
      this.editType = '';
      this.currEditInfo = {};
      this.hideUploadBtn = false;
      this.imageList = [];
    },
    mounted() {
      if (this.editType === 'edit') {
        this.currEditInfo = { ...this.editInfo }
        const imageList = this.currEditInfo.pictureArray ? this.currEditInfo.pictureArray.split(',') : [];
        this.imageList = imageList.map((item, index) => {
          return { name: `图${index}`, url: item }
        })
      }
    },
    methods:{
      onUpload(obj,res,file,idx){
        if (obj.code == 200) {
          this.$message.success({dangerouslyUseHTMLString: true, message: obj.msg})
          this.handleImageList(file)
        } else {
          this.$message.error('操作失败')
        }
      },
      handleImageList(fileList) {
        this.currEditInfo.pictureArray = '';
        this.imageList = fileList.map((item, index) => {
          const url = (item['response'] && item['response']['data'] && item['response']['data']['fileUrl']) || item.url;
          this.currEditInfo.pictureArray += url + (index === fileList.length - 1 ? '' : ',')
          return { name: item.name, url }
        })
        this.hideUploadBtn = this.imageList.length >= 6;
      },
      onRemoveImageList(file, fileList) {
        this.handleImageList(fileList);
      },
      async onSave() {
        const { data } = await saveTemplateDataList(this.currEditInfo);
        if (data.code === 200) {
          this.$message({
            message: '保存成功',
            type: 'success',
          });
          this.$emit('onCloseToggle', { isSave: true })
        } else {
          this.$message({
            message: data.msg || '模版保存失败',
            type: 'error',
            showClose: true,
            duration: 3000,
          });
        }
      },
      onClose() {
        this.$emit('onCloseToggle', {})
      }
    },
}
</script>
<style scoped>
.template_detail /deep/.el-upload--picture-card, /deep/.el-upload-list__item {
  width: 100px !important;
  height: 100px !important;
  line-height: 100px !important;
}
</style>
